package com.uniqueyi.baselib.frgbase;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.uniqueyi.baselib.R;
import com.uniqueyi.baselib.baseInterface.RefreshListener;

/**
 * Created by
 * author:  cwy.
 * date:    2018/3/2.  9:36.
 * prj:     Uniqueyi.
 */


public abstract class BaseFrg extends Fragment implements View.OnClickListener{

    protected Context mContext;

    View loading_view;
    View load_retry_view;
    View childView;
    private FrameLayout frg_content;
    private RefreshListener refreshListener;

    public void setRetryRefreshItr(RefreshListener refreshListener) {
        this.refreshListener = refreshListener;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        mContext = getActivity();
        return inflater.inflate(R.layout.base_frg, container, false);
    }
    @Override public void onAttach(Activity activity) {
        super.onAttach(activity);
        this.mContext = activity;
    }

    @Override public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initBaseView(view);
        //绑定视图
        initView();
        //处理业务
        initData();
        //事件
        initEvent();

    }

    protected  void initBaseView(View view){
        frg_content =  view.findViewById(R.id.frg_content);

        childView = View.inflate(mContext, getLayout(), null);
        frg_content.addView(childView);

        loading_view = View.inflate(mContext, R.layout.loading_view, null);
        load_retry_view = View.inflate(mContext, R.layout.load_retry_view, null);
        frg_content.addView(loading_view);
        frg_content.addView(load_retry_view);

        loading_view.setVisibility(View.GONE);
        load_retry_view.setVisibility(View.GONE);

        load_retry_view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //重新读取数据
                hideReTry();
                if (refreshListener != null) {
                    refreshListener.reLoadData();
                }
            }
        });
    }

    //刷新
    public abstract int getLayout();
    public abstract void initView();
    public abstract void initData();
    public abstract void initEvent();

    protected <T extends View> T findView(int id) {
        return (T) getView().findViewById(id);
    }


    public void showReTry() {
        load_retry_view.setVisibility(View.VISIBLE);
    }

    public void showLoading() {
        loading_view.setVisibility(View.VISIBLE);
    }

    public void hideReTry() {
        load_retry_view.setVisibility(View.GONE);
    }

    public void hideLoading() {
        loading_view.setVisibility(View.GONE);
    }

}
